// pages/my/my.js
var nickName = ''//用户名字
var avatarUrl = ''//用户头像
var nickNameBut=true//登录状态
var pageNumber = ''//页数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order:[],//订单
    icon:[
      'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/icons/about.png',
      'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/icons/activity.png',
      'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/icons/demeanour.png',
      'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/icons/order.png',
      'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/icons/posters.png',
      'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/icons/service.png'
    ],
  },
  onLoad: function (options) {
    var that=this;
    var order = this.data.order
    // 判断用户是否登录
    wx.getSetting({
    success:function(res){
      if (res.authSetting['scope.userInfo']){
        // 获取用户信息
        wx.getUserInfo({
          success: function (res) {
            that.setData({
              nickName: res.userInfo.nickName,
              avatarUrl: res.userInfo.avatarUrl,
              nickNameBut:true
            })
          }
        }),
        //用户登录
        wx.login({
          success(res) {
            //缓存数据
            wx.setStorage({
              key: 'key',
              data: res.code
            })
          },
        })
      }else{
        that.setData({
          nickNameBut:false
        })
      }
    }
    })
    //网路请求
    wx.request({
      url: 'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/json/order.json',
      method:'GET',
      header:{
        'content-type': 'application/json'
      },
      success:function(res){
        that.setData({
          order : res
        })
      },fail:function(res){

      }
    })
  },
  //点击获得授权
  getUser(e) {
    wx.getUserInfo({
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          nickNameBut:true,
        });
        wx.login({
          success(res) {
            //缓存数据
            wx.setStorage({
              key: 'key',
              data: res.code
            })
          },
        })
        //页面刷新
        const pages = getCurrentPages()
        const perpage = pages[pages.length - 1]
        perpage.onLoad()  
      }
    })
  },
  //点击拨打电话
  CallPhone:function(res){
    wx.makePhoneCall({
      // success: function (res) {
        // console.log(res)
        phoneNumber: '15760163770'
      // },
      // fail: function (res) {
      //   wx.showModal({
      //     title: '',
      //     content: '失败',
      //     showCancel: false
      //   })
      // }
    })
  },

  //判断是否登录，然后跳转
  goto:function(res){
    var that=this;
    pageNumber = res.currentTarget.dataset.current
    //用户登录状态
    wx.getStorage({
      key: 'key',
      success: function(res) {
        //console.log(res.data)
        if (res.data != '' && res.data != 'res.code' && res.data !=undefined){
          that.jump();
        }else{
          console.log("失败")
        }
      },fail:function(res){
        console.log(res.data)
        wx.showModal({
          content: '您没有登录，是否登录？',
          success: function (res) {
            if (res.confirm) {
              //console.log("成功")
              this.jump();
            } else if (res.cancel) {
              //console.log("失败")
            }
          },fail:function(){
            //console.log("失败")测试
          },
        })
      }
    })
  },
  product:function(res){
    var id= 1;
    wx.setStorage({
      key: 'page',
      data: id,
    }),
    wx.navigateTo({
      url: 'OrderDetails/OrderDetails',
    })
  },

  //页面跳转
  jump:function(res){
      // var id = this.res.currentTarget.dataset.current
      var id = pageNumber
    if (id=='0'){
      wx.setStorage({
        key: 'page',
        data: id,
      }),
      wx.navigateTo({
        url: 'OrderDetails/OrderDetails',
      })
    } else if (id == '1'){
      wx.setStorage({
        key: 'page',
        data: id,
      }),
      wx.navigateTo({
        url: 'OrderDetails/OrderDetails',
      })
    } else if (id == '2') {
      wx.setStorage({
        key: 'page',
        data: id,
      }),
      wx.navigateTo({
        url: 'OrderDetails/OrderDetails',
      })
    } else if (id == '3') {
      wx.setStorage({
        key: 'page',
        data: id,
      }),
      wx.navigateTo({
        url: 'OrderDetails/OrderDetails',
      })
    } else if (id == '4') {
      wx.setStorage({
        key: 'page',
        data: id,
      }),
      wx.navigateTo({
        url: 'OrderDetails/OrderDetails',
      })
    } else if (id == '5') {
      wx.setStorage({
        key: 'page',
        data: id,
      }),
      wx.navigateTo({
          url: 'OrderDetails/OrderDetails',
      })
    }
  },

  // 现在客服
  PopupWindow:function(res){

  },
  // 未签到
  wqd: function (e) {
    this.setData(
      {
        showModal: true
      }
    )
  },
  ok: function () {
    this.setData({
      showModal: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})